import Cookie from 'js-cookie'
export default {
    state: {
        isCollapse: false,
        currentMenu: null,
        menu: [],
        tabs: [
            {
                path: "/",
                name: "home",
                lable: "首页",
                icon: "home"
            }
        ]
    },
    mutations: {
        setMenu(state, val) {
            state.menu = val
            Cookie.set('menu', JSON.stringify(val))
            // console.log(val);
        },
        clearMenu(state) {
            state.menu = []
            Cookie.remove('menu')
        },
        addMenu(state, router) {
            let menu = JSON.parse(Cookie.get('menu'))
            if (!menu) {
                return
            }
            state.menu = menu
            let currentMenu = [
                {
                    path: '/',
                    component: () => import('@/views/Main'),
                    children: []
                }
            ]
            menu.forEach(item => {
                if (item.children) {
                    item.children = item.children.map(item => {
                        item.component = () => import(`@/views/${item.url}`)
                        return item
                    })
                    currentMenu[0].children.push(...item.children)
                } else {
                    item.component = () => import(`@/views/${item.url}`)
                    currentMenu[0].children.push(item)
                }
            });
            // console.log(currentMenu, 'cur');
            router.addRoutes(currentMenu)
        },
        seletMeun(state, vaule) {
            // vaule.name === 'home' ? state.currentMenu = null : state.currentMenu = vaule
            if (vaule.name !== 'home') {
                state.currentMenu = vaule
                let result = state.tabs.findIndex(item => item.name === vaule.name)
                result === -1 ? state.tabs.push(vaule) : ''
            } else {
                state.currentMenu = null
            }
        },
        closeTab(state, vaule) {
            let result = state.tabs.findIndex(item => item.name === vaule.name)
            state.tabs.splice(result, 1)
        },
        collapseMeun(state) {
            state.isCollapse = !state.isCollapse;
        }
    },
    actions: {
    },
}
